package com.demo.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.config.TicketFeignClient;
import com.demo.dao.MovieProductDao;
import com.demo.exetion.MainExection;
import com.demo.model.MovieProduct;
import com.demo.model.MovieScore;
import com.demo.model.MovieTicketing;
import com.demo.service.MovieProductService;
import com.demo.service.MovieScoreService;
import com.demo.utils.CommonUtil;
import com.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务,可以加在启动类上
public class ScheduledTask {

    @Resource
    private MovieProductDao movieProductDao;
    @Resource
    private MovieProductService movieProductService;

    @Resource
    private TicketFeignClient ticketFeignClient;

    @Resource
    private MovieScoreService movieScoreService;

    @Autowired
    private RedisTemplate<String, Object> ort;

    //票房定时任务
    @Scheduled(cron = "0 0 3 * * ?")
    public void boxOffice() throws MainExection {
        System.out.println("票房定时任务开始执行");
        //查询所有上线的电影
        QueryWrapper<MovieProduct> movieProductQueryWrapper=new QueryWrapper<>();
        //上级的电影
        movieProductQueryWrapper.eq("movie_shelf",0);
        List<MovieProduct> movieProducts = movieProductDao.selectList(movieProductQueryWrapper);
        //对票房中的redis进行修改
        int number=0;
        List<MovieProduct> movieProductsList=new ArrayList<>();
        for (MovieProduct movieProduct : movieProducts) {
            //获取redis中的票房数据
            Object o = ort.opsForValue().get("Movie:id:" + movieProduct.getProductId());
            //判断
            if(o!=null){
                movieProduct.setMovieBoxOffice(o.toString());
                number++;
                movieProductsList.add(movieProduct);
            }
            if (number==100){
                //修改票房
                boolean b = movieProductService.updateBatchById(movieProductsList);
                if (!b) throw new MainExection(500,"修改票房失败");
                movieProductsList.clear();
                number=0;
            }
        }
        //最后对不满足100条的数据进行修改
        if (number!=0){
            boolean b = movieProductService.updateBatchById(movieProductsList);
            if (!b) throw new MainExection(500,"修改票房失败");
        }
        System.out.println("票房定时任务执行结束");
    }
    //评分定时任务
    @Scheduled(cron = "0 0 3 * * ?")
    public void score() throws MainExection {
        Result<List<MovieTicketing>> listResult = ticketFeignClient.selectTicketimgs();
        if (listResult.getCode()!=200)throw new MainExection(500,"查询票务数据失败");
        List<MovieTicketing> movieTicketings = listResult.getData();
        List<MovieScore> movieScores=new ArrayList<>();
        for (MovieTicketing movieTicketing : movieTicketings) {
            Object o = ort.opsForValue().get("score:Ticketing_id" +movieTicketing.getTicketingId());
            if (o==null){
                MovieScore movieScore = MovieScore.builder().score(5)
                        .ticketingId(movieTicketing.getTicketingId())
                        .orderId(movieTicketing.getOrderId())
                        .productId(movieTicketing.getProductId())
                        .createTime(CommonUtil.dataLocalDatePares()).build();
                ort.opsForValue().set("score:Ticketing_id" + movieTicketing.getTicketingId(),"5");
                movieScores.add(movieScore);
            }
        }
        if (movieScores!=null && movieScores.size()>0){
            boolean b = movieScoreService.saveBatch(movieScores);
            if (!b)throw new MainExection(500,"定时任务修改票房失败");
        }
        //修改评分
        List<MovieProduct> list = movieProductService.list();
        for (MovieProduct movieProduct : list) {
            QueryWrapper<MovieScore> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("product_Id",movieProduct.getProductId());
            List<MovieScore> movieScores1 = movieScoreService.list(queryWrapper);
            Double score=0.0;
            int i=0;
            if (movieScores1 !=null && movieScores1.size()>0){
                for (MovieScore movieScore : movieScores1) {
                    score+=movieScore.getScore();
                    i++;
                }
            }else {
                continue;
            }
            Double newScore=score/i;
            movieProduct.setMovieScore(newScore.toString());
        }
        boolean b1 = movieProductService.updateBatchById(list);
        if (!b1)throw new MainExection(500,"定时任务修改票房失败");
    }


}
